package com.googlecode.wheres_my_stuff_okay_potato;

import java.util.LinkedList;
import java.util.List;

import android.app.Activity;
import android.os.Bundle;
import android.view.Menu;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemSelectedListener;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.Spinner;

/**
 * Adds GUI and functionality for searching by category.
 * 
 */
public class SearchByCategory extends Activity implements
		OnItemSelectedListener {
	List<Item> butts = new LinkedList<Item>();

	private void filterList(String category) {
		final Global GLOBAL = (Global) getApplication();
		List<Item> breet = GLOBAL.getItemsByCat(category);
		List<String> breetStrings = new LinkedList<String>();

		for (Item i : breet)
			breetStrings.add(i.toString());

		ListView found_item_lv = (ListView) findViewById(R.id.cat_search_lv);
		ArrayAdapter<String> adapter2 = new ArrayAdapter<String>(
				SearchByCategory.this, android.R.layout.simple_list_item_1,
				android.R.id.text1, breetStrings);
		found_item_lv.setAdapter(adapter2);
	}

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// final Button confcat = (Button) findViewById(R.id.confcat);
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_search_by_category);
		final Spinner spinner = (Spinner) findViewById(R.id.catospinny);
		spinner.setOnItemSelectedListener(this);
		// Create an ArrayAdapter using the string array and a default spinner
		// layout
		ArrayAdapter<CharSequence> adapter = ArrayAdapter.createFromResource(
				this, R.array.categories_array,
				android.R.layout.simple_spinner_item);
		// Specify the layout to use when the list of choices appears
		adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
		// Apply the adapter to the spinner
		spinner.setAdapter(adapter);

		/*
		 * confcat.setOnClickListener(new OnClickListener() { public void
		 * onClick(View v) { butts =
		 * GLOBAL.getItemsByCat(spinner.getSelectedItem().toString());
		 * List<String> item_names = new LinkedList<String>();
		 * 
		 * for(Item item : butts) item_names.add(item.toString());
		 * 
		 * final ListView found_item_lv =
		 * (ListView)findViewById(R.id.cat_search_lv); //ArrayAdapter<String>
		 * adapterz = new ArrayAdapter<String>(this,
		 * android.R.layout.simple_list_item_1, android.R.id.text1, item_names);
		 * } });
		 */
	}

	/**
	 * Inflate the menu; this adds items to the action bar if it is present.
	 */
	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		getMenuInflater().inflate(R.menu.search_by_category, menu);
		return true;
	}

	public void onItemSelected(AdapterView<?> parent, View view, int pos,
			long id) {
		// An item was selected. You can retrieve the selected item using
		// parent.getItemAtPosition(pos)
		filterList(parent.getItemAtPosition(pos).toString());

	}

	@Override
	public void onNothingSelected(AdapterView<?> parent) {
		// Another interface callback
	}
}
